using System.Reflection;
using UnityEngine;
using UnityEngine.Serialization;

namespace HEFramework
{
    public class DataTableSettingScriptableObject : ScriptableObject
    {
        private static DataTableSettingScriptableObject instance;

        public static DataTableSettingScriptableObject Instance
        {
            get
            {
                if (instance != null)
                {
                    return instance;
                }

                instance = Resources.Load<DataTableSettingScriptableObject>(DATA_TABLE_SETTING_SAVE_FILE_NAME);
                if (instance != null)
                {
                    return instance;
                }

                instance = CreateInstance<DataTableSettingScriptableObject>();

                instance.ResetAll();

#if UNITY_EDITOR

                if (!System.IO.Directory.Exists(DATA_TABLE_SETTING_SAVE_PATH))
                {
                    System.IO.Directory.CreateDirectory(DATA_TABLE_SETTING_SAVE_PATH);
                    UnityEditor.AssetDatabase.Refresh();
                }

                UnityEditor.AssetDatabase.CreateAsset(instance, DATA_TABLE_SETTING_SAVE_FILE_PATH);
#endif
                return instance;
            }
        }


        /// <summary>
        /// 数据体存储名字
        /// </summary>
        private const string DATA_TABLE_SETTING_SAVE_FILE_NAME = "DataTableSettings";

        /// <summary>
        /// 数据体文件后缀
        /// </summary>
        private const string DATA_TABLE_SETTING_FILE_EXTENSION = ".asset";

        /// <summary>
        /// 数据体保存路径
        /// </summary>
        private const string DATA_TABLE_SETTING_SAVE_PATH = "Assets/Resources";

        /// <summary>
        /// 数据体文件保存路径
        /// </summary>
        private const string DATA_TABLE_SETTING_SAVE_FILE_PATH = "Assets/Resources/" + DATA_TABLE_SETTING_SAVE_FILE_NAME + DATA_TABLE_SETTING_FILE_EXTENSION;


        /// <summary>
        /// 表格文件加载路径
        /// </summary>
        [SerializeField] private string dataTablePath;

        /// <summary>
        /// 导出数据保存路径
        /// </summary>
        [SerializeField] private string saveDataPath;

        /// <summary>
        /// 导出代码保存路径
        /// </summary>
        [SerializeField] private string saveCodePath;

        /// <summary>
        /// 导出key保存文件
        /// </summary>
        [SerializeField] private string saveOutputKeyPath;

        /// <summary>
        /// 成功后是否等待
        /// </summary>
        [SerializeField] private bool isLogDetail;

        /// <summary>
        /// 是否替换换行符以及回车符
        /// </summary>
        [SerializeField] private bool isReplaceTableAndEnterChar;

        /// <summary>
        /// 忽略以此为开始的文件
        /// </summary>
        [SerializeField] private string ignoreFileWhenStartWith;

        /// <summary>
        /// 忽略以此为开始的sheet表格
        /// </summary>
        [SerializeField] private string ignoreSheetWhenStartWith;

        /// <summary>
        /// 是否忽略以"#"为开头的列
        /// </summary>
        [SerializeField] private bool isIgnoreColumnWhichFirstRowIsHashTag;

        /// <summary>
        /// 是否忽略以"#"为开头的行
        /// </summary>
        [SerializeField] private bool isIgnoreRowWhichFirstColumnIsHashTag;

        /// <summary>
        /// 是否忽略以""为开头的行
        /// </summary>
        [SerializeField] private bool isIgnoreRowWhichFirstPropertyIsEmpty;

        /// <summary>
        /// 设置导出行
        /// </summary>
        [SerializeField] private int propertySettingRow;

        /// <summary>
        /// 名字导出行
        /// </summary>
        [SerializeField] private int propertyNameRow;

        /// <summary>
        /// 类型导出行
        /// </summary>
        [SerializeField] private int propertyTypeRow;

        /// <summary>
        /// 节点导出行
        /// </summary>
        [SerializeField] private int propertyNoteRow;

        /// <summary>
        /// 数据开始行
        /// </summary>
        [SerializeField] private int propertyStartRow;

        /// <summary>
        /// 数组标记
        /// </summary>
        [SerializeField] private string propertyArrEndWith;

        /// <summary>
        /// 数组数据分隔标志
        /// </summary>
        [SerializeField] private char propertyArrSplitChar;


        public string DataTablePath
        {
            get { return dataTablePath; }
            set
            {
                if (dataTablePath == value)
                {
                    return;
                }

                dataTablePath = value;
                Modified = true;
            }
        }

        public string SaveDataPath
        {
            get { return saveDataPath; }
            set
            {
                if (saveDataPath == value)
                {
                    return;
                }

                saveDataPath = value;
                Modified = true;
            }
        }

        public string SaveCodePath
        {
            get { return saveCodePath; }
            set
            {
                if (saveCodePath == value)
                {
                    return;
                }

                saveCodePath = value;
                Modified = true;
            }
        }

        public string SaveOutputKeyPath
        {
            get { return saveOutputKeyPath; }
            set
            {
                if (saveOutputKeyPath == value)
                {
                    return;
                }

                saveOutputKeyPath = value;
                Modified = true;
            }
        }

        public bool IsLogDetail
        {
            get { return isLogDetail; }
            set
            {
                if (isLogDetail == value)
                {
                    return;
                }

                isLogDetail = value;
                Modified = true;
            }
        }

        public bool IsReplaceTableAndEnterChar
        {
            get { return isReplaceTableAndEnterChar; }
            set
            {
                if (isReplaceTableAndEnterChar == value)
                {
                    return;
                }

                isReplaceTableAndEnterChar = value;
                Modified = true;
            }
        }

        public string IgnoreFileWhenStartWith
        {
            get { return ignoreFileWhenStartWith; }
            set
            {
                if (ignoreFileWhenStartWith == value)
                {
                    return;
                }

                ignoreFileWhenStartWith = value;
                Modified = true;
            }
        }

        public string IgnoreSheetWhenStartWith
        {
            get { return ignoreSheetWhenStartWith; }
            set
            {
                if (ignoreSheetWhenStartWith == value)
                {
                    return;
                }

                ignoreSheetWhenStartWith = value;
                Modified = true;
            }
        }

        public bool IsIgnoreColumnWhichFirstRowIsHashTag
        {
            get { return isIgnoreColumnWhichFirstRowIsHashTag; }
            set
            {
                if (isIgnoreColumnWhichFirstRowIsHashTag == value)
                {
                    return;
                }

                isIgnoreColumnWhichFirstRowIsHashTag = value;
                Modified = true;
            }
        }

        public bool IsIgnoreRowWhichFirstColumnIsHashTag
        {
            get { return isIgnoreRowWhichFirstColumnIsHashTag; }
            set
            {
                if (isIgnoreRowWhichFirstColumnIsHashTag == value)
                {
                    return;
                }

                isIgnoreRowWhichFirstColumnIsHashTag = value;
                Modified = true;
            }
        }

        public bool IsIgnoreRowWhichFirstPropertyIsEmpty
        {
            get { return isIgnoreRowWhichFirstPropertyIsEmpty; }
            set
            {
                if (isIgnoreRowWhichFirstPropertyIsEmpty == value)
                {
                    return;
                }

                isIgnoreRowWhichFirstPropertyIsEmpty = value;
                Modified = true;
            }
        }

        public int PropertySettingRow
        {
            get { return propertySettingRow; }
            set
            {
                if (propertySettingRow == value)
                {
                    return;
                }

                propertySettingRow = value;
                Modified = true;
            }
        }

        public int PropertyNameRow
        {
            get { return propertyNameRow; }
            set
            {
                if (propertyNameRow == value)
                {
                    return;
                }

                propertyNameRow = value;
                Modified = true;
            }
        }

        public int PropertyTypeRow
        {
            get { return propertyTypeRow; }
            set
            {
                if (propertyTypeRow == value)
                {
                    return;
                }

                propertyTypeRow = value;
                Modified = true;
            }
        }

        public int PropertyNoteRow
        {
            get { return propertyNoteRow; }
            set
            {
                if (propertyNoteRow == value)
                {
                    return;
                }

                propertyNoteRow = value;
                Modified = true;
            }
        }

        public int PropertyStartRow
        {
            get { return propertyStartRow; }
            set
            {
                if (propertyStartRow == value)
                {
                    return;
                }

                propertyStartRow = value;
                Modified = true;
            }
        }

        public string PropertyArrEndWith
        {
            get { return propertyArrEndWith; }
            set
            {
                if (propertyArrEndWith == value)
                {
                    return;
                }

                propertyArrEndWith = value;
                Modified = true;
            }
        }

        public char PropertyArrSplitChar
        {
            get { return propertyArrSplitChar; }
            set
            {
                if (propertyArrSplitChar == value)
                {
                    return;
                }

                propertyArrSplitChar = value;
                Modified = true;
            }
        }


        /// <summary>
        /// 是否储存上一次变更的属性 以便于回退操作
        /// </summary>
        private bool Modified
        {
            set
            {
#if UNITY_EDITOR
                //如果外部传值 就保存上一次数值
                if (value)
                {
                    UnityEditor.EditorUtility.SetDirty(this);
                }
#endif
            }
        }


        private Assembly asmb;

        public Assembly Asmb
        {
            get
            {
                if (asmb == null)
                {
                    // 获取程序集
                    string aPath = System.IO.Directory.GetParent(Application.dataPath).ToString();
                    aPath += @"\Library\ScriptAssemblies\Assembly-CSharp.dll";
                    asmb = Assembly.LoadFrom(aPath);
                }

                return asmb;
            }
        }


        /// <summary>
        /// 重置全部
        /// </summary>
        public void ResetAll()
        {
            dataTablePath = @"";
            saveDataPath = @"";
            saveCodePath = @"";
            saveOutputKeyPath = @"";
            isLogDetail = true;
            isReplaceTableAndEnterChar = true;
            ignoreFileWhenStartWith = "~$";
            ignoreSheetWhenStartWith = "_";
            isIgnoreColumnWhichFirstRowIsHashTag = true;
            isIgnoreRowWhichFirstColumnIsHashTag = true;
            isIgnoreRowWhichFirstPropertyIsEmpty = true;
            propertySettingRow = 0;
            propertyNameRow = 1;
            propertyTypeRow = 2;
            propertyNoteRow = 3;
            propertyStartRow = 4;
            propertyArrEndWith = "[]";
            propertyArrSplitChar = '|';
            Modified = true;
        }
    }
}